/**
 * 
 */
package org.softTank.wsdlParser;

import java.io.File;
import java.io.StringReader;
import java.net.URL;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.softTank.wsdlParser.bean.Definitions;
import org.softTank.wsdlParser.bean.Message;
import org.softTank.wsdlParser.bean.PartnerLinkType;
import org.softTank.wsdlParser.bean.PortType;
import org.softTank.wsdlParser.bean.Types;

/**
 * @author Yuzone
 *
 */
public class WsdlParserImpl implements WsdlParser {

	protected Definitions definitions;
	protected JAXBContext context;
	protected Unmarshaller unmarshaler;
	protected Logger logger;

	public WsdlParserImpl() {
		PropertyConfigurator.configure("main/resource/log4j.properties");
		logger = Logger.getLogger(this.getClass());
		try {
			// create unmarshaller
			context = JAXBContext.newInstance("org.softTank.wsdlParser.bean");
			unmarshaler = context.createUnmarshaller();
		} catch (JAXBException e) {
			logger.debug("Catch exception in creating unmarshaller: "
					+ e.getMessage());
		}
	}

	public WsdlParserImpl(File file) throws JAXBException {
		this();
		definitions = (Definitions) unmarshaler.unmarshal(file);
	}

	public WsdlParserImpl(URL url) throws JAXBException {
		this();
		definitions = (Definitions) unmarshaler.unmarshal(url);
	}

	public WsdlParserImpl(String wsdlContent) throws JAXBException {
		this();
		definitions = (Definitions) unmarshaler.unmarshal(new StringReader(
				wsdlContent));
	}

	/* (non-Javadoc)
	 * @see org.softTank.wsdlParser.WsdlParser#getDefinitions()
	 */
	@Override
	public Definitions getDefinitions() {
		return definitions;
	}

	/* (non-Javadoc)
	 * @see org.softTank.wsdlParser.WsdlParser#getMessages()
	 */
	@Override
	public List<Message> getMessages() {
		return definitions.getMessage();
	}

	/* (non-Javadoc)
	 * @see org.softTank.wsdlParser.WsdlParser#getPartnerLinkTypes()
	 */
	@Override
	public List<PartnerLinkType> getPartnerLinkTypes() {
		return definitions.getPartnerLinkType();
	}

	/* (non-Javadoc)
	 * @see org.softTank.wsdlParser.WsdlParser#getPortTypes()
	 */
	@Override
	public List<PortType> getPortTypes() {
		return definitions.getPortType();
	}

	/* (non-Javadoc)
	 * @see org.softTank.wsdlParser.WsdlParser#parser()
	 */
	@Override
	public void parser() {

	}

	/* (non-Javadoc)
	 * @see org.softTank.wsdlParser.WsdlParser#setSource(java.io.File)
	 */
	@Override
	public void setSource(File wsdl) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.softTank.wsdlParser.WsdlParser#getTypes()
	 */
	@Override
	public Types getTypes() {
		return definitions.getTypes();
	}

}
